<template>
  <div class="layout">
    <el-container>
      <el-header>
        <div class="header-content">
          <router-link to="/" class="logo">校园活动管理系统</router-link>
          <el-menu
            mode="horizontal"
            :router="true"
            :default-active="activeMenu"
            class="main-menu"
          >
            <el-menu-item index="/activities">
              <el-icon><Calendar /></el-icon>
              活动管理
            </el-menu-item>
            <el-menu-item index="/articles">
              <el-icon><Document /></el-icon>
              文章管理
            </el-menu-item>
            <el-menu-item index="/gifts">
              <el-icon><Present /></el-icon>
              礼品管理
            </el-menu-item>
            <el-menu-item index="/activity-types">
              <el-icon><Collection /></el-icon>
              活动类型
            </el-menu-item>
          </el-menu>
          <div class="user-menu">
            <el-dropdown trigger="click">
              <span class="user-dropdown-link">
                <el-icon><User /></el-icon>
                {{ username }}
                <el-icon><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleLogout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      
      <el-main>
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import {
  Calendar,
  Document,
  Present,
  User,
  ArrowDown,
  SwitchButton,
  Collection
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

const username = ref(localStorage.getItem('username') || '用户')
const activeMenu = computed(() => route.path)

const handleLogout = async () => {
  try {
    // 清除认证状态
    authStore.clearAuth()
    
    // 清除本地存储
    localStorage.removeItem('token')
    localStorage.removeItem('username')
    
    ElMessage.success('退出成功')
    
    // 确保在消息显示后再跳转
    await router.push('/login')
  } catch (error) {
    console.error('Logout failed:', error)
    ElMessage.error('退出失败，请重试')
  }
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
}

.el-header {
  background: var(--primary-color);
  padding: 0;
  box-shadow: var(--box-shadow);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 1rem;
}

.logo {
  color: white;
  font-size: 1.5rem;
  font-weight: bold;
  text-decoration: none;
  margin-right: 2rem;
}

.main-menu {
  flex: 1;
  background: transparent;
  border: none;
}

.main-menu :deep(.el-menu-item) {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  height: 60px;
  line-height: 60px;
  transition: all 0.3s ease;
  position: relative;
}

.main-menu :deep(.el-menu-item.is-active) {
  color: white;
  background: rgba(255, 255, 255, 0.25);
  font-weight: bold;
  font-size: 17px;
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.main-menu :deep(.el-menu-item.is-active)::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 4px;
  background: white;
  border-radius: 2px 2px 0 0;
}

.main-menu :deep(.el-menu-item:hover) {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.main-menu :deep(.el-menu-item .el-icon) {
  margin-right: 8px;
  font-size: 20px;
  vertical-align: middle;
}

.main-menu :deep(.el-menu-item.is-active .el-icon) {
  transform: scale(1.1);
}

.user-menu {
  margin-left: 2rem;
}

.user-dropdown-link {
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.el-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem 1rem;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 